import {getToken,getRefreshToken,setToken, setRefreshToken, removeToken, removeRefreshToken}  from '@/utils/cache';
import {setSessionStore, getSessionStore} from "../../utils/store";
import {login, getQXList} from "../../request/api/user";
import {unzip} from "../../utils/utils";

const user = {
  state: {
    token: getToken(),
    refreshToken: getRefreshToken(),
    isLogin: getSessionStore('isLogin') || false,

    permission: getSessionStore('permission'),
    menu: getSessionStore('menu')|| [],

    companyName: getSessionStore('companyName'),
    companyId: getSessionStore('companyId'),
    userName: getSessionStore('userName'),
    userId: getSessionStore('userId'),
    departmentName: getSessionStore('departmentName'),
    departmentId: getSessionStore('departmentId'),
    employeeRowId: getSessionStore('employeeRowId')
  },
  actions: {
    login({ commit }, data){
      return new Promise((resolve, reject) =>{
        login(data).then(res =>{
          console.log(res);
          if (res.errcode != 0){
            reject();
            return
          }
          let data = res.row[0];
          let {token,refresh_token,company_name,company_id,user_name,user_id,department_id,department_name,employee_row_id} = data;

          commit('SET_TOKEN', token);
          commit('SET_REFRESH_TOKEN', refresh_token);
          commit('SET_LOGIN', true);
          commit('SET_COMPANY_NAME', company_name);
          commit('SET_COMPANY_ID',company_id);
          commit('SET_USER_NAME',user_name);
          commit('SET_USER_ID', user_id);
          commit('SET_DEPARTMENT_NAME', department_name);
          commit('SET_DEPARTMENT_ID', department_id);
          commit('SET_EMPLOYEE_ROW_ID', employee_row_id);
          resolve()
        })
      });
    },

    logout({ commit}){
      removeToken();
      removeRefreshToken();

      commit('SET_TOKEN', '');
      commit('SET_REFRESH_TOKEN', '');
      commit('SET_LOGIN', false);
      commit('SET_COMPANY_NAME', '');
      commit('SET_COMPANY_ID','');
      commit('SET_USER_NAME','');
      commit('SET_USER_ID', '');
      commit('SET_DEPARTMENT_NAME', '');
      commit('SET_DEPARTMENT_ID', '');
      commit('SET_EMPLOYEE_ROW_ID', '');
      commit('SET_PERMISSION','')
    },

    getAuth({commit}){
      let data = [
        {
          "system_id":1001,
          "function_id":"user",
          "function_name":"用户管理",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"role",
          "function_name":"角色管理",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"permission",
          "function_name":"角色权限",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"employeeInf",
          "function_name":"员工管理",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"department",
          "function_name":"公司架构",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"zhangTaoHeSuanQuYu",
          "function_name":"账套核算区域",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"zhangTaoCaoZuoYuanGongMingXi",
          "function_name":"账套核算区域明细",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"operation",
          "function_name":"营运组",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"parameter",
          "function_name":"系统参数",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"agreement",
          "function_name":"协议管理",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"identificationType",
          "function_name":"证件类型",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"stop",
          "function_name":"站点管理",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"leaveSchoolType",
          "function_name":"回程接送方式",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"seatStatus",
          "function_name":"座位状态",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"shuttleDirection",
          "function_name":"班车方向",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"schoolType",
          "function_name":"站点类型",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"ticketPackageType",
          "function_name":"套票类型",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"ticketPackageLabel",
          "function_name":"套票标签",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"ticketType",
          "function_name":"车票类型",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"shuttle",
          "function_name":"班车管理",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"shuttlePackage",
          "function_name":"班车套票",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"shuttleStop",
          "function_name":"班车站点",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":false,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"shuttleSeat",
          "function_name":"班车座位",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"shuttleSchool",
          "function_name":"班车站点",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"customer",
          "function_name":"客户管理",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"passengers",
          "function_name":"乘车人管理",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"order",
          "function_name":"订单管理",
          "parent_id":"",
          "function_type":"表",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"exportOrder",
          "function_name":"导出订单",
          "parent_id":"order",
          "function_type":"字段",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"exportPhoto",
          "function_name":"导出乘车人照片",
          "parent_id":"order",
          "function_type":"字段",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"exportSchool",
          "function_name":"导出站点总名单",
          "parent_id":"order",
          "function_type":"字段",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"exportGo",
          "function_name":"导出班车去程名单",
          "parent_id":"order",
          "function_type":"字段",
          "is_see":true,
          "is_append":true,
          "is_edit":true,
          "is_delete":true,
        },
        {
          "system_id":1001,
          "function_id":"exportLeave",
          "function_name":"导出班车回程名单",
          "parent_id":"order",
          "function_type":"字段",
          "is_see":true,
          "is_append": true,
          "is_edit":true,
          "is_delete":true,
        },
      ]
      return new Promise(resolve => {
        getQXList({}).then(res =>{
          if (res.errcode !== 0) {
            resolve();
            return
          }
          let data = res.row;
          commit('SET_PERMISSION',data)
          resolve();
        })

        // commit('SET_PERMISSION',data)
        // resolve();
      })
    }
  },
  mutations: {
    SET_TOKEN: (state,token) => {
      setToken(token);
      setSessionStore('access_token',token)
      state.token = token;
    },
    SET_REFRESH_TOKEN: (state, refreshToken) => {
      setRefreshToken(refreshToken);
      setSessionStore('refresh_token',refreshToken)
      state.refreshToken = refreshToken;
    },
    SET_LOGIN: (state,type) =>{
      setSessionStore('isLogin',type)
      state.isLogin = type;
    },
    SET_COMPANY_NAME: (state,name)=>{
      state.companyName = name;
      setSessionStore('companyName',name)
    },
    SET_COMPANY_ID: (state,id)=>{
      state.companyId = id;
      setSessionStore('companyId',id)
    },
    SET_USER_NAME: (state,name)=>{
      state.userName = name;
      setSessionStore('userName',name)
    },
    SET_USER_ID: (state,id)=>{
      state.userId = id;
      setSessionStore('userId',id)
    },
    SET_DEPARTMENT_NAME: (state,name)=>{
      state.departmentName = name;
      setSessionStore('departmentName',name)
    },
    SET_DEPARTMENT_ID: (state,id)=>{
      state.departmentId = id;
      setSessionStore('departmentId',id)
    },
    SET_EMPLOYEE_ROW_ID: (state,id)=>{
      state.employeeRowId = id;
      setSessionStore('employeeRowId',id)
    },
    SET_PERMISSION: (state,obj) => {
      state.permission = obj;
      setSessionStore('permission',obj)

    },
    SET_MENU: (state,menu) =>{
      setSessionStore('menu',menu);
      state.menu = menu
    }
  },
  getters: {
    getMenus: state =>  state.menu
  }
}
export default user

/**/
